/***********************************************************************
 slategen.cpp - Main module of automatic WickDD slate bitmap generator.

 Created 2006.06.10 by Warren Young

 Copyright (c) 2006 by Pixel Corps.  This program may be used under
 the terms of the X11 license, a copy of which should have accompanied
 this program, in the LICENSE file.
***********************************************************************/

#include "FrameBuffer.h"
#include "SlateInfo.h"

#include <SDL.h>

#include <getopt.h>

#include <exception>
#include <iostream>

using namespace std;


//// CreateSlate ///////////////////////////////////////////////////////

static void
CreateSlate(SlateInfo& si, FrameBuffer::ImageType eImageType)
{
	si.TrimAllWS();
	
	FrameBuffer frame;
	frame.WriteText("Project:", si.GetProject());
	frame.WriteText("Date:", si.GetDate());
	frame.WriteText("Scene:", si.GetScene());
	frame.WriteText("Element:", si.GetElement());
	frame.WriteText("Artist:", si.GetArtist());
	frame.WriteText("Notes:", si.GetNotes());
	frame.WriteText("File:", si.GetFirstFile());

	string sFileName(si.FileName());
	string::size_type pos = sFileName.find(".xml");
	if (pos == sFileName.length() - 4) {
		sFileName.resize(sFileName.length() - 4);
	}

	frame.WriteImage(sFileName.c_str(), eImageType);
}


//// Usage /////////////////////////////////////////////////////////////

static void
Usage()
{
	fprintf(stderr, "usage: slategen [-t {PNG|TIFF}] filename\n\n");
	fprintf(stderr, "    If -t is given, it sets the image type to use "
			"for the slate; defaults\n");
	fprintf(stderr, "    to TIFF. Argument case is ignored.\n\n");
	fprintf(stderr, "    filename is the name of the XML file "
			"containing the slate info.  If\n");
	fprintf(stderr, "    the filename contains \".xml\", that is "
			"replaced with .png or .tif, as\n");
	fprintf(stderr, "    appropriate. Otherwise, .png or .tif is "
			"appended to the file name.\n");
	fprintf(stderr, "    This argument is required.\n\n");
}


//// ParseCommandLine //////////////////////////////////////////////////

static bool
ParseCommandLine(int argc, char* const argv[],
		FrameBuffer::ImageType& eImageType)
{
	int ch;
	while ((ch = getopt(argc, argv, "t:")) != EOF) {
		switch (ch) {
			case 't':
				if (strcasestr(optarg, "tif") != 0) {
					eImageType = FrameBuffer::kImage_TIFF;
				}
				else if (strcasestr(optarg, "png") != 0) {
					eImageType = FrameBuffer::kImage_PNG;
				}
				else {
					Usage();
					return false;
				}
				break;

			default:
				Usage();
				return false;
		}
	}
	
	return true;
}


//// main //////////////////////////////////////////////////////////////

int
main(int argc, char *argv[])
{
	FrameBuffer::ImageType eImageType = FrameBuffer::kImage_TIFF;
	if (ParseCommandLine(argc, argv, eImageType)) {
		if (optind < argc) {
			try {
				SlateInfo si(argv[optind]);
				CreateSlate(si, eImageType);
				return 0;
			}
			catch (const exception& e) {
				cerr << "Failure: " << e.what() << endl;
				return 1;
			}
		}
		else {
			Usage();
			return 2;
		}
	}
	else {
		return 3;
	}
}

